package com.gpf.server;

import java.io.*;
import java.net.Socket;


public class ServerHandle implements Runnable{
    private Socket socket;
    public ServerHandle(Socket socket){
        this.socket = socket;
    }
    public void run() {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try{
            // 装饰模式
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(),true);
            String body = null;
            while (true){
                body = reader.readLine();
                if(body == null){
                    break;
                }
                System.out.println("Receive : "+ body);
                writer.println("hello, " + body);
            }
        }catch (Exception e){
            e.printStackTrace();
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (writer != null) {
                writer.close();
            }
            if (this.socket != null) {
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                this.socket = null;
            }

        }
    }
}
